#include "stdafx.h"
#include "MenuScreen.hpp"
#include "KeyboardInput.hpp"

MenuScreen::MenuScreen(void)
{
}


MenuScreen::~MenuScreen(void)
{
}

void MenuScreen::Initialize()
{
	_menuState = MenuState::PlayersSelection;

	_mainTexture.loadFromFile("..\\Resources\\menuSpritesheet.gif");
	_background = rh::Frame(&_mainTexture, sf::IntRect(0,0,256,224));
	_players[0] = rh::Frame(&_mainTexture, sf::IntRect(137,229,28,24));
	_players[1] = rh::Frame(&_mainTexture, sf::IntRect(184,230,29,23));
	_playersCursor = rh::Frame(&_mainTexture, sf::IntRect(222, 231, 10, 16));
	_difficulties[0] = rh::Frame(&_mainTexture, sf::IntRect(20, 229, 52, 12));
	_difficulties[1] = rh::Frame(&_mainTexture, sf::IntRect(76, 229, 39, 12));
	_difficulties[2] = rh::Frame(&_mainTexture, sf::IntRect(48, 244, 39, 9));
	_difficultiesCursor = rh::Frame(&_mainTexture, sf::IntRect(27,243, 16,10));


	_players[0].setPosition(sf::Vector2f(95,175));
	_players[1].setPosition(sf::Vector2f(135,175));
	_difficulties[0].setPosition(sf::Vector2f(81,170));
	_difficulties[1].setPosition(sf::Vector2f(137,170));
	_difficulties[2].setPosition(sf::Vector2f(109,186));

	UpdateMenu();
	MoveCursor();
}

void MenuScreen::Draw(sf::RenderWindow& renderWindow, const sf::RenderStates& renderState)
{
	_background.Draw(renderWindow, renderState);
	_players[0].Draw(renderWindow, renderState);
	_players[1].Draw(renderWindow, renderState);
	_playersCursor.Draw(renderWindow, renderState);

	_difficulties[0].Draw(renderWindow, renderState);
	_difficulties[1].Draw(renderWindow, renderState);
	_difficulties[2].Draw(renderWindow, renderState);
	_difficultiesCursor.Draw(renderWindow, renderState);
}

void MenuScreen::Update(const sf::Time& elapsedTime, const sf::Time& totalElapsedTime)
{
	switch(_menuState)
	{
	case MenuState::PlayersSelection:
		HandlePlayerInput();
		break;
	case MenuState::DifficultySelection:
		HandleDifficultyInput();
		break;
	}
	UpdateMenu();
	MoveCursor();
}

void MenuScreen::ManagePlayerSelection(const sf::Time& elapsedTime, const sf::Time& totalElapsedTime)
{
}

void MenuScreen::HandlePlayerInput()
{
	if(rh::KeyboardInput::GetKeyState(sf::Keyboard::A) == rh::KeyState::Released)
		_menuState = MenuState::DifficultySelection;
	if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Left) == rh::KeyState::Released
		|| rh::KeyboardInput::GetKeyState(sf::Keyboard::Right) == rh::KeyState::Released)
	{
		if(_gameSettings.GetGameMode() == GameSettings::GameMode::TwoPlayers)
			_gameSettings.SetGameMode(GameSettings::GameMode::OnePlayer);
		else
			_gameSettings.SetGameMode(GameSettings::GameMode::TwoPlayers);
	}
}

void MenuScreen::HandleDifficultyInput()
{
	if(rh::KeyboardInput::GetKeyState(sf::Keyboard::A) == rh::KeyState::Released)
		SettingsUpdated.Emit(_gameSettings);
	if(rh::KeyboardInput::GetKeyState(sf::Keyboard::B) == rh::KeyState::Released)
		_menuState = MenuState::PlayersSelection;
	switch(_gameSettings.GetDifficultySetting())
	{
	case GameSettings::DifficultySetting::Begginer:
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Right) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Expert);
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Down) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Intermediate);
		break;
	case GameSettings::DifficultySetting::Expert:
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Left) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Begginer);
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Down) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Intermediate);
		break;	
	case GameSettings::DifficultySetting::Intermediate:
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Left) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Begginer);
		if(rh::KeyboardInput::GetKeyState(sf::Keyboard::Right) == rh::KeyState::Released)
			_gameSettings.SetDifficultySetting(GameSettings::DifficultySetting::Expert);
		break;
	}
}

void MenuScreen::MoveCursor()
{
	switch(_menuState)
	{
	case MenuState::PlayersSelection:
		_difficultiesCursor.setVisibility(false);
		_playersCursor.setVisibility(true);
		switch(_gameSettings.GetGameMode())
		{
		case GameSettings::GameMode::OnePlayer:
			_playersCursor.setPosition(sf::Vector2f(85, 175));
			break;
		case GameSettings::GameMode::TwoPlayers:
			_playersCursor.setPosition(sf::Vector2f(125, 175));
			break;
		}
		break;
	case MenuState::DifficultySelection:
		_difficultiesCursor.setVisibility(true);
		_playersCursor.setVisibility(false);
		switch(_gameSettings.GetDifficultySetting())
		{
		case GameSettings::DifficultySetting::Begginer:
			_difficultiesCursor.setPosition(sf::Vector2f(88,185));
			break;
		case GameSettings::DifficultySetting::Expert:
			_difficultiesCursor.setPosition(sf::Vector2f(145,185));
			break;
		case GameSettings::DifficultySetting::Intermediate:
			_difficultiesCursor.setPosition(sf::Vector2f(117,201));
			break;
		}
		break;
	}
}

void MenuScreen::UpdateMenu()
{
	switch(_menuState)
	{
	case MenuState::PlayersSelection:
		_difficulties[0].setVisibility(false);
		_difficulties[1].setVisibility(false);
		_difficulties[2].setVisibility(false);
		_difficultiesCursor.setVisibility(false);
		_playersCursor.setVisibility(true);

		_players[0].setVisibility(true);
		_players[1].setVisibility(true);

		break;
	case MenuState::DifficultySelection:
		_difficulties[0].setVisibility(true);
		_difficulties[1].setVisibility(true);
		_difficulties[2].setVisibility(true);
		_difficultiesCursor.setVisibility(true);
		_playersCursor.setVisibility(false);
		_players[0].setVisibility(false);
		_players[1].setVisibility(false);
		break;
	}
}